﻿using AbpTool.Utils;
using Blazorise.Snackbar;
using Microsoft.AspNetCore.Components;

namespace AbpTool.Components.Pages
{
    public partial class Coder
    {
        public string _sql = string.Empty;
        public bool _processing = false;
        private List<string> _properties = new List<string>();
        private List<string> _parseCode = new List<string>();
        private List<string> _mudTables = new List<string>();


        private void ConvertToCharpModel()
        {
            _processing = true;
            try
            {
                if (_sql.IsBlank())
                {
                    //snackbar.Add("SQL语句不能为空", Severity.Warning);
                    return;
                }
                var columns = _sql.Substring(_sql.IndexOf("SELECT", StringComparison.OrdinalIgnoreCase) + 6, _sql.IndexOf("FROM", StringComparison.OrdinalIgnoreCase) - 6)
                    .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(c => c.Trim().Split(' ')[0].ToPascalCase())
                    .ToList();
                _properties.Clear();
                _mudTables.Clear();
                _mudTables.Add("<MudTable Items=\"@_items\" @bind-SelectedItems=\"_selectedItems\" Dense=\"true\" Hover=\"true\" Bordered=\"true\" Striped=\"true\" MultiSelection=\"true\" CustomHeader=\"true\" CustomFooter=\"true\">");
                _mudTables.Add("<ToolBarContent>");
                _mudTables.Add("<MudText Typo=\"Typo.h6\">Items</MudText>");
                _mudTables.Add("<MudSpacer />");
                _mudTables.Add("</ToolBarContent>");
                _mudTables.Add("<HeaderContent>");
                _mudTables.Add("<MudTHeadRow Checkable=\"true\">");

                var mudTablesTemp = new List<string>();
                var index = 0;
                foreach (var item in columns)
                {
                    if (item == "Id")
                    {
                        _properties.Add("public long? Id { get; set; }");
                        _parseCode.Add($"{item} = item[{index++}].GetString(),");
                    }
                    else if (item.Contains("Date") || item.Contains("Time"))
                    {
                        _properties.Add($"public DateTime? {item} {{ get; set; }}");
                        _parseCode.Add($"{item} = item[{index++}].GetString(),");
                    }

                    else if (item.StartsWith("Is"))
                    {
                        _properties.Add($"public bool? {item} {{ get; set; }}");
                        _parseCode.Add($"{item} = item[{index++}].GetInt16(),");
                    }
                    else
                    {
                        _properties.Add($"public string? {item} {{ get; set; }}");
                        _parseCode.Add($"{item} = item[{index++}].ToString(),");
                    }

                    _mudTables.Add($"<MudTh>{item}</MudTh>");
                    mudTablesTemp.Add($"<MudTd DataLabel=\"{item}\">@context.{item}</MudTd>");
                }
                _mudTables.Add("<MudTh>Action</MudTh>");
                _mudTables.Add($"</MudTHeadRow>");
                _mudTables.Add($"</HeaderContent>");
                _mudTables.Add($"<RowTemplate>");
                mudTablesTemp.Add($"<MudTd DataLabel=\"Action\"><MudButton OnClick=\"() => ShowEvd(context.FilePath)\" Variant=\"Variant.Filled\" Color=\"Color.Info\">查看</MudButton></MudTd>");
                _mudTables.AddRange(mudTablesTemp);
                _mudTables.Add($"</RowTemplate>");
                _mudTables.Add("<FooterContent>");
                _mudTables.Add("<MudTFootRow Checkable=\"true\">");
                _mudTables.Add("<MudTd colspan=\"5\">Selected: @_selectedEvds?.Count</MudTd>");
                _mudTables.Add("</MudTFootRow>");
                _mudTables.Add("</FooterContent>");
                _mudTables.Add("<PagerContent>");
                _mudTables.Add("<MudTablePager />");
                _mudTables.Add("</PagerContent>");
                _mudTables.Add("</MudTable>");
                //snackbar.Add("SQL语句已转换为C#模型", Severity.Success);
            }
            catch (Exception ex)
            {
                //snackbar.Add($"转换失败: {ex.Message}", Severity.Error);
            }
            finally
            {
                _processing = false;
            }
        }

        private void ClearData()
        {
            _properties.Clear();
            _mudTables.Clear();
            //snackbar.Add("已清除数据", Severity.Info);
        }
    }
}
